package com.keta.foundation.log;

import javax.servlet.http.HttpServletRequest;

import com.keta.foundation.constants.Constants;

public abstract class LogUtil {

	// 用于存储每个线程的request请求
	private static final ThreadLocal<HttpServletRequest> LOCAL_REQUEST = new ThreadLocal<HttpServletRequest>();

	/**
	 * AOP拦截器执行方法前调用该方法，将请求放入ThreadLocal
	 */
	public static void putRequest(HttpServletRequest request) {
		LOCAL_REQUEST.set(request);
	}

	/**
	 * AOP拦截器执行方法后调用该方法，移除request
	 */
	public static void romeveRequest() {
		LOCAL_REQUEST.remove();
	}

	/**
	 * 将参数放入LOG_ARGUMENTS。
	 */
	public static void putArgs(Object[] objects) {
		HttpServletRequest request = LOCAL_REQUEST.get();
		request.setAttribute(Constants.LOG_ARGUMENTS, objects);
	}

	/**
	 * 得到参数
	 */
	public static Object[] getArgs() {
		HttpServletRequest request = LOCAL_REQUEST.get();
		return (Object[]) request.getAttribute(Constants.LOG_ARGUMENTS);
	}

	public static void main(String[] args) {
		String str = "";
		long starTime = System.currentTimeMillis();
		// 计算循环10000的时间
		for (int i = 0; i < 10000; i++) {
			str = str + i;
		}
		long endTime = System.currentTimeMillis();
		long Time = endTime - starTime;
		System.out.println(Time);
		StringBuilder bulider = new StringBuilder("");
		starTime = System.currentTimeMillis();
		for (int j = 0; j < 10000; j++) {
			bulider.append(j);
		}
		endTime = System.currentTimeMillis();
		Time = endTime - starTime;
		System.out.println(Time);
	}

}
